using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Primitives;
using Microsoft.OpenApi.Models;
using TodoMVC.BusinessServices;
using TodoMVC.DBServices;

namespace TodoMVC.Main
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }


        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<TodoMVCContext>(options =>
            {
                //内存数据库，用于测试很方便
                //options.UseInMemoryDatabase("TodoMVC");
                var connectionString = ConnectionHelper.GetSqliteConnectionString();
                options.UseSqlite(connectionString, options =>
                 {
                     options.CommandTimeout(10);
                     options.MaxBatchSize(10);
                 });
            });

            services.AddTransient<TodoMVCDBServices, TodoMVCDBServices>();
            services.AddTransient<TodoMVCBusinessServices, TodoMVCBusinessServices>();
            services.AddTransient<SqliteDBBusinessService, SqliteDBBusinessService>();
            services.AddControllers();

            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("v1", new OpenApiInfo
                {
                    Title = "TodoMVC API",
                    Version = "v1",
                    License = new OpenApiLicense() { Name = "MIT" }
                });
                //options.IncludeXmlComments();
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            //开启 swagger 文档，访问地址：http://localhost/api-docs
            app.UseSwagger(options =>
            {
                options.RouteTemplate = "api-docs/{documentName}/swagger.json";
            });

            app.UseSwaggerUI(options =>
            {
                options.RoutePrefix = "api-docs";
                options.SwaggerEndpoint("/api-docs/v1/swagger.json", "TodoMVC API v1");
            });

            // 设置wwwroot下的默认访问页列表，即访问一个路径后没有指定具体文件的时候，应该访问哪个文件
            // 默认值 default.html,default.htm,index.html index.htm
            var defaultFilesOptions1 = new DefaultFilesOptions();
            //额外添加三个默认值
            defaultFilesOptions1.DefaultFileNames.Add("index.txt");
            defaultFilesOptions1.DefaultFileNames.Add("default.txt");
            defaultFilesOptions1.DefaultFileNames.Add("readme.md");
            defaultFilesOptions1.RequestPath = "";
            //defaultFilesOptions
            app.UseDefaultFiles(defaultFilesOptions1);
            // 设置 clients 下的默认访问页列表
            var defaultFilesOptions2 = new DefaultFilesOptions();
            //额外添加三个默认值
            defaultFilesOptions2.DefaultFileNames.Add("index.txt");
            defaultFilesOptions2.DefaultFileNames.Add("default.txt");
            defaultFilesOptions2.DefaultFileNames.Add("readme.md");
            defaultFilesOptions2.RequestPath = "/clients";
            //defaultFilesOptions
            app.UseDefaultFiles(defaultFilesOptions2);

            //开启静态文件访问
            //访问 wwwroot
            var staticFileOptions1 = new StaticFileOptions()
            {
                RequestPath = "",
                OnPrepareResponse = ctx =>
                {
                    ctx.Context.Response.Headers.Add("Area-ID", "wwwroot");
                }
            };
            app.UseStaticFiles(staticFileOptions1);
            //访问 clients
            var staticFileOptions2 = new StaticFileOptions()
            {
                RequestPath = "/clients",
                FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "clients")),
                OnPrepareResponse = ctx =>
                {
                    ctx.Context.Response.Headers.Add("Area-ID", "clients");
                }
            };
            app.UseStaticFiles(staticFileOptions2);

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }

    }
}
